﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace DotNetNuke.Modules.ActiveForums.Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:UserStats runat=server></{0}:UserStats>")> _
Public Class UserStats
        Inherits WebControl

        Private _template As DisplayTemplate
        Private _userId As Integer = -1
        Private _moduleId As Integer = -1
        Public Property Template() As DisplayTemplate
            Get
                Return _template
            End Get
            Set(ByVal value As DisplayTemplate)
                _template = value
            End Set
        End Property
        Public Property UserId() As Integer
            Get
                Return _userId
            End Get
            Set(ByVal value As Integer)
                Try
                    _userId = value
                Catch ex As Exception
                    _userId = -1
                End Try

            End Set
        End Property
        Public Property ModuleId() As Integer
            Get
                Return _moduleId
            End Get
            Set(ByVal value As Integer)
                _moduleId = value
            End Set
        End Property
        Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
            If UserId = -1 Then
                Exit Sub
            End If
            Try
                Dim output As String = String.Empty
                Dim ps As DotNetNuke.Entities.Portals.PortalSettings = CType(Context.Items("PortalSettings"), DotNetNuke.Entities.Portals.PortalSettings)
                Dim cu As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo()
                Dim imagePath As String = String.Empty
                Dim portalId As Integer = ps.PortalId
                Dim tmp As String = String.Empty
                If Template Is Nothing Then
                    tmp = "<span class=""aslabelsmbold"">[RESX:Posts]:</span> [AF:PROFILE:POSTCOUNT]<br />" & _
                            "<span class=""aslabelsmbold"">[RESX:RankName]:</span> [AF:PROFILE:RANKNAME]<br />" & _
                            "<span class=""aslabelsmbold"">[RESX:RankDisplay]:</span> [AF:PROFILE:RANKDISPLAY] <br />" & _
                            "<span class=""aslabelsmbold"">[RESX:LastUpdate]:</span> [AF:PROFILE:DATELASTACTIVITY:d] <br />" & _
                            "<span class=""aslabelsmbold"">[RESX:MemberSince]:</span> [AF:PROFILE:DATECREATED:d]"
                Else
                    tmp = Template.Text
                End If
                If ModuleId = -1 Then
                    Dim mc As New DotNetNuke.Entities.Modules.ModuleController
                    Dim tc As New DotNetNuke.Entities.Tabs.TabController
                    Dim ti As DotNetNuke.Entities.Tabs.TabInfo = Nothing
                    For Each mi As DotNetNuke.Entities.Modules.ModuleInfo In mc.GetModules(portalId)
                        If mi.DesktopModule.ModuleName.ToUpperInvariant = "Active Forums".ToUpperInvariant Then
                            ModuleId = mi.ModuleID
                            Exit For
                        End If
                    Next
                End If
                output = TemplateUtils.ParseProfileTemplate(tmp, UserId, portalId, ModuleId, cu.UserID, ps.TimeZoneOffset)
                output = Utilities.LocalizeControl(output)
                writer.Write(output)
            Catch ex As Exception
                writer.Write(ex.Message)
            End Try
            
        End Sub

    End Class

End Namespace

